package com.lib.util;


import android.app.Application;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author qinbaowei
 * @date 2017/8/18
 * @email qbaowei@qq.com
 */

public class DateTimeUtil {

    private static Application sContext;

    public static void init(Application context) {
        sContext = context;
    }

    private static final String[] DAY_IN_WEEK = new String[]{
            "周日", "周一", "周二", "周三", "周四", "周五", "周六"
    };
    private static final String[] DAY_IN_WEEK_1 = new String[]{
            "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"
    };

    public static String formatYYYYMMDDToday() {
        return formatYYYYMMDD(getServerCurrentTimeMillis());
    }

    public static String formatTimeChinese(long time) {
        return new SimpleDateFormat("yyyy年MM月dd日").format(new Date(time)) + " " + getDayInWeek1(time);
    }

    public static String formatYYYYMMDD_Chinese(long time) {
        return new SimpleDateFormat("yyyy年MM月dd日").format(time);
    }

    public static String formatYYYYMMDD_Chinese(int year, int month, int date) {
        return String.format("%04d年%02d月%02d日", year, month, date);
    }

    public static String formatYYYYMMDD(int year, int month, int date) {
        return String.format("%04d-%02d-%02d", year, month, date);
    }

    public static String formatYYYYMMDD(long time) {
        return new SimpleDateFormat("yyyy-MM-dd").format(time);
    }

    public static String formatMMDDHHMM(long time) {
        return new SimpleDateFormat("MM-dd HH:mm").format(time);
    }

    public static String formatHHMM(long time) {
        return new SimpleDateFormat("HH:mm").format(new Date(time));
    }

    public static String formatDD(long time) {
        return new SimpleDateFormat("dd").format(new Date(time));
    }

    public static String formatYYYYMMDDHHMM(long time) {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date(time));
    }

    public static String formatMagicyoTime(long time) {
        if (time == 0) {
            return "--";
        }
        String strTime = "";
        int[] currYmd = getCurrentYMD();
        int[] ymd = getYMD(time);
        int[] hm = getHHMM(time);
        if (ymd[0] == currYmd[0]) {
            if (ymd[1] == currYmd[1] && ymd[2] == currYmd[2]) {
                strTime = String.format("%02d:%02d", hm[0], hm[1]);
            } else {
                strTime = String.format("%02d-%02d %02d:%02d", ymd[1], ymd[2], hm[0], hm[1]);
            }
        } else {
            strTime = String.format("%04d-%02d-%02d %02d:%02d",
                                    ymd[0],
                                    ymd[1],
                                    ymd[2],
                                    hm[0],
                                    hm[1]);
        }
        return strTime;
    }

    public static String formatTime(long time) {
        return formatTime(time, null);
    }

    public static String formatTime(long time, SimpleDateFormat dateFormat) {
        if (time <= 0) {
            return ResUtil.getString(R.string.util_minute_ago);
        }
        String formatTime = "";
        long millis = getServerCurrentTimeMillis() - time;// 得出的时间间隔是毫秒
        if (millis / 1000 < 60) {
            //显示1分钟前
            formatTime = ResUtil.getString(R.string.util_minute_ago);
        } else if (millis / 60000 < 60 && millis / 60000 > 0) {
            // 如果时间间隔小于60分钟则显示多少分钟前
            int m = (int) ((millis % 3600000) / 60000);// 得出的时间间隔的单位是分钟
            formatTime = String.format(ResUtil.getString(R.string.util_minute_ago_format), m);
        } else if (millis / 3600000 < 24 && millis / 3600000 > 0) {
            // 如果时间间隔小于24小时则显示多少小时前
            int h = (int) (millis / 3600000);// 得出的时间间隔的单位是小时
            formatTime = String.format(ResUtil.getString(R.string.util_hour_ago_format), h);
        } else if (isTimeInThisYear(time)) {
            // 大于24小时，在今年内，则显示正常的时间，但是不显示秒
            if (dateFormat != null) {
                formatTime = dateFormat.format(new Date(time));
            } else { //如果传过来的匹配方式为空,使用默认匹配
                formatTime = formatMMDDHHMM(time);
            }
        } else {
            // 不在今年内，则显示正常的时间，但是不显示秒
            formatYYYYMMDDHHMM(time);
        }
        return formatTime;
    }

    public static boolean isTimeInThisYear(long time) {

        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(time);
        int year = c.get(Calendar.YEAR);
        if (year - Calendar.getInstance().get(Calendar.YEAR) == 0) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean isTodayYYYYMMDD(long today, long time) {
        return formatYYYYMMDD(today).equals(formatYYYYMMDD(time));
    }

    public static int[] getYMD(long time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);
        return new int[]{
                calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH) + 1,
                calendar.get(Calendar.DATE)
        };
    }

    public static int[] getHHMM(long time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);
        return new int[]{
                calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE)
        };
    }

    public static int[] getCurrentYMD() {
        return getYMD(getServerCurrentTimeMillis());
    }

    public static int getCurrentM() {
        return getCurrentYMD()[1];
    }

    public static int getYear(long time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(time));
        return calendar.get(Calendar.YEAR);
    }

    public static int getMonth(long time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(time));
        return calendar.get(Calendar.MONTH) + 1;
    }

    public static int getDate(long time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(time));
        return calendar.get(Calendar.DATE);
    }

    public static String getDayInWeek(long time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(time));
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        return DAY_IN_WEEK[calendar.get(Calendar.DAY_OF_WEEK) - 1];
    }

    public static String getDayInWeek1(long time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(time));
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        return DAY_IN_WEEK_1[calendar.get(Calendar.DAY_OF_WEEK) - 1];
    }

    public static String getDayInWeek1(int year, int month, int date) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, date);
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        return DAY_IN_WEEK_1[calendar.get(Calendar.DAY_OF_WEEK) - 1];
    }

    /**
     * @return time2距离time1有几天
     */
    public static long getDaysOff(long time1, long time2) {
        long time = time2 - time1;
        long oneDay = 86400000;//一天有几毫秒
        long remainSecond = time % oneDay / 1000;
        return time / oneDay + (remainSecond > 1 ? 1 : 0);
    }

    /**
     * 获取一个月有几天
     */
    public static int getTotalDayInThisMonth(int year, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        return calendar.getActualMaximum(Calendar.DATE);
    }

    public static Date parseYYYYMMDD(String formatTime) {
        return parse(formatTime, "yyyy-MM-dd");
    }

    private static Date parse(String formatTime, String pattern) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            Date date = sdf.parse(formatTime);
            return date;
        } catch (ParseException e) {
            System.out.println(e.getMessage());
        }
        return new Date();
    }

    public static long getServerCurrentTimeMillis() {
        if (sContext instanceof IDateTimeUtil) {
            return ((IDateTimeUtil) sContext).getServerCurrentTimeMillis();
        }
        return System.currentTimeMillis();
    }

    public static long getTimestampByYearMonth(int year, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.DAY_OF_MONTH, 0);
        return calendar.getTimeInMillis();
    }

    public interface IDateTimeUtil {
        long getServerCurrentTimeMillis();
    }
}
